package org.phoenixframework.api.course.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.phoenixframework.api.course.service.ISysUserService;
import org.phoenixframework.common.entity.SysUser;
import org.phoenixframework.common.entity.annotation.OperationLog;
import org.phoenixframework.common.tools.PasswordEncoderUtil;
import org.phoenixframework.common.tools.ResultModel;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author doublelife
 * @since 2021-05-08
 */
@Slf4j
@Api(tags = {"系统用户"})
@AllArgsConstructor
@RestController
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {

    private final ISysUserService sysUserService;

    @OperationLog
    @ApiOperation(value = "保存用户信息")
    @PostMapping("/save")
    public ResultModel<?> save(@ApiParam(name = "sysUser", value = "用户信息", required = true) @Validated @RequestBody SysUser sysUser) {
        sysUser.setPassword(PasswordEncoderUtil.encodeByBCrypt(sysUser.getPassword()));
        boolean result = sysUserService.save(sysUser);
        return this.buildByBool(result);
    }

    @OperationLog
    @ApiOperation(value = "根据id获取一条用户信息")
    @GetMapping("/find/{id}")
    public ResultModel<?> findById(@ApiParam(name = "id", value = "用户Id", required = true) @PathVariable Long id) {
        SysUser sysUser = sysUserService.getById(id);
        sysUser.setPassword(null);
        return this.buildByObject(sysUser);
    }

    @OperationLog
    @ApiOperation(value = "修改用户")
    @PutMapping("/update")
    public ResultModel<?> updateById(@ApiParam(name = "sysUser", value = "用户信息", required = true) @Validated @RequestBody SysUser sysUser) {
        if (StringUtils.isBlank(sysUser.getId().toString())) {
            return ResultModel.error();
        }
        boolean result = sysUserService.updateById(sysUser);
        return this.buildByBool(result);
    }

    @OperationLog
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/remove/{id}")
    public ResultModel<?> removeById(@ApiParam(name = "id", value = "用户Id", required = true) @PathVariable Long id) {
        boolean b = sysUserService.removeById(id);
        return this.buildByBool(b);
    }

    @OperationLog
    @ApiOperation(value = "根据id列表批量删除")
    @DeleteMapping("/removeByIds")
    public ResultModel<?> removeByIds(@RequestBody List<String> ids) {
        boolean b = sysUserService.removeByIds(ids);
        return this.buildByBool(b);
    }


}
